Defining XPath Expressions |
|
To control the expression evaluation, you can pass dynamic information such as, variable binding, namespace binding, and so on to the XPath execution context with the help of XPathMetaInfo object. This XPathMetaInfo object can be shared across different XPath objects.
The different options for using XPathMetaInfo Object are:
- Variable binding
- Namespace binding
- Function registration
Variable Binding
XPath expressions can contain variable reference that requires a value to be assigned to it. For example, the expression //personalInfo[@name=$var], will select the personalInfo elements having the value of the name attribute equal to the value of the variable reference var. To get the expression evaluated, the variable var has to be assigned a value. Here is the algorithm for doing the same:
XPath xpathObject = new XPath("/personalInfo[@name = $var]"); XPathMetaInfo xpathMetaInfoObj = new XPathMetaInfo(); XPathResult XpathResultObj = new XPathResult("Toma"); XPathMetaInfoObj.addVarBinding("var" , XpathResultObj); XPathResult res = XpathObject.evaluate(iNomNode, XpathMetaInfoObj);
Note: In the above example, iNomNode refers to the NOM handle containing the XML.
Namespace Binding
Similar to variable binding, you can bind a URI to a prefix used in an XPath expression:
XPath xpathObject = new XPath("/ns1:onalInfo[@name = $var]"); XPathMetaInfo xpathMetaInfoObj = new XPathMetaInfo(); XPathMetaInfoObj.addNamespaceBinding("ns1", "http://hello.com"); XPathResult res = XpathObject.evaluate(iNomNode, XpathMetaInfoObj);
When you use namespace specific search, ensure the following:
- Use a prefix inside the XPath expressions to find a specific element that is originating from a particular namespace. The prefix could either be the actual prefix used in the input document or the default namespace (no prefix). But the prefix you use inside the expression should be mapped to the actual URI that you expect inside the input document.
- For performance reasons, the namespace processing is disabled by default. However, if you use a prefix inside XPath expression, it will enable the namespace processing automatically. The expression will match nodes if the local part of the element name is matched. For example, consider following expression,
"/mycat/name"
<cat xmlns="http://cat.com"> <mycat> <name>Toma</name> </mycat> </cat>
<ns:cat xmlns:ns="http://cat.com"> <ns:mycat> <ns:name>Toma</ns:name> </ns:mycat> </ns:cat>
"/ns1:mycat/ns1:name"
Function Registration
Most of the functions described by XPath1.0 and XSLT1.0 are available in Process Platform. However, for a list of missing functions, refer to Features that are not supported in Process Platform.
However, if you want to use custom functions, you must register them (for native users) from the native layer. These functions must be called from XPath expressions, using MetaInfo object. Users can associate a name with a function pointer of the type,
void (*lib_fun) ( void *pXpathContext , CXPathResult *pArg[], CXPathResult *retValue);
Note: The retValue is used to pass the result back to the engine. Initially, it will contain the number of arguments actually passed through the expression. For using custom Java functions, though a separate registration is not needed from the Java layer, the following conditions must be satisfied :
- Java function must be static
- The class must be set in the classpath
- The Java method call can return Nodeset however, the Nodeset must include only those nodes which are part of the input NOM tree originally provided
According to the XPath1.0 specification, the XPath function id(Object) selects nodes by their unique identity (ID).
NOM does not have ID support, but XPath specification has ID aspect. As a limited work around, an user can have a particular attribute designated as the ID attribute during XPath evaluation by using the following API of theXPathMetaInfoclass:
registerAttribute(java.lang.StringsAttr)
This makes the XPath processor consider the registered attribute as the ID attribute and select nodes accordingly.